/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

// DlgMain.h : header file

#pragma once

#include "MConfig.h"
#include "MConfigInterface.h"
#include "MSkin.h"
#include "RgnWindow.h"
#include "DlgSplash.h"
#include "DlgMap.h"
#include "DlgMsg.h"
#include "DlgDetails.h"
#include "DlgSelScanCfg.h"
#include "DlgInterface.h"
#include "DlgConfig.h"
#include "DlgHunt.h"
#include "afxwin.h"
#include "WorkCfg.h"
#include "TheScanner.h"
#include "WorkCfgManager.h"
#include "MMsgDisplay.h"
#include "MKOuiIabList.h"
#include "MKMcast4List.h"
#include "MPortDescriptionList.h"
#include "MHelpInfo.h"
#include "MUtilsSO.h"
#include "MCCLabel.h"
#include "MCVersionReader.h"
#include "MEventLog.h"
#include "MPopupMenu.h"
#include "MUpdateCheck.h"
#include "ScanMethodHuntParams.h"

/*! \brief The mode in which the application should run
*/
enum CHScannerRunMode
{
	__rmNormal,
	__rmAutoScan,
	__rmPassive,
	__rmError
};

/*! \brief The class behind the main window
*/
class DlgMain : public CDialog
{
private:
	MHelpInfo helpInfo;

	MConfig          config;
	MConfigInterface configInterface;
	bool LoadConfiguration(void);
	bool SaveConfiguration(void);

	ScanMethodHuntParams xHuntParams;
	MIPLocator ipLocator;
	MIPLocatorBin ipLocatorBin;
	bool LoadIpLocatorBinData(void);

	MKOuiIabList         vendorList;
	MKMcast4List         mcast4NamesList;
	MPortDescriptionList portDescList;

	MSkin skin;
	bool LoadCurrentSkin(void);

	DlgSplash *dlgSplash;
	bool CreateSplashDialog(void);
	void DestroySplashDialog(void);

	bool CreateUI(void);
	void DestroyUI(void);
		DlgMap *dlgMap;
		bool CreateMapDialog(void);
		void DestroyMapDialog(void);

		DlgMsg *dlgMsg;
		bool CreateMsgDialog(void);
		void DestroyMsgDialog(void);

		DlgDetails *dlgDetails;
		bool CreateDetailsDialog(void);
		void DestroyDetailsDialog(void);

		CButton uiBtnSniff;
		CButton uiBtnScan;
		CButton uiBtnHunt;
		CButton uiBtnStop;
		CButton uiBtnExpXML;
		CButton uiBtnIntf;
		CButton uiBtnTools;
		CButton uiBtnHelp;
		CButton uiBtnClose;
		CButton uiBtnMinimize;
		CButton uiBtnGPL;
		MCCLabel staInfo;
		MCCLabel staGPL;
	void PositionUiElements(void);

	void SetUiScanStarted(void);
	void SetUiPassiveScanStarted(void);
	void SetUiHuntScanStarted(void);
	void SetUiWait(void);
	void SetUiNormal(void);
	void BeepItUp(void);

	DlgSelScanCfg dlgSelScanCfg;
	DlgInterface  dlgInterface;
	DlgEdit       dlgEdit;
	DlgConfig     dlgConfig;
	DlgHunt       dlgHunt;

	WorkCfg workCfg;

	TheScanner theScanner;
	bool WaitForScanToFinish(void);

	bool firstTime;

	MMsgDisplay display;

	WorkCfgManager wcm;

	LastScanType lastScanType;

	MUtilsSO utilsSO;
	MCVersionReader versionReader;

	void SetWorkInterfaceAndDisplayInfo(void);
	CString GetProgramNameAndVersion(void);
	void SetDisplayInformations(void);

	void ShowLoadedInfoCounters(void);

	int autoRunTime;

	void AutoScan(void);
	void AutoScanPassive(void);

	int interfaceCount;

	void ToolsMACLookup(void);
	void ToolsMcast4Lookup(void);
	void ToolsConfigureApplication(void);
	void ToolsResetLayout(void);
	void ToolsCheckForUpdates(void);
	void ToolsImportGeoIP(void);

	MUpdateCheck updateCheck;

protected:
	HICON m_hIcon;
	HICON hIconMinimize;
	HBITMAP hBmpGPL;

	MPopupMenu popupMenu;
	bool SetPopupMenu(int);
	void ProcessMenuItemCommand(WORD);

	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support

	// Generated message map functions
	virtual BOOL OnInitDialog();
	virtual void OnCancel();
	virtual void OnOK();

	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();
	afx_msg void OnSize(UINT nType, int cx, int cy);
	afx_msg void OnBnClickedBtnScan();
	afx_msg void OnBnClickedBtnSniff();
	afx_msg void OnBnClickedBtnHunt();
	afx_msg void OnBnClickedBtnStop();
	afx_msg void OnBnClickedBtnExpXML();
	afx_msg void OnBnClickedBtnIntf();
	afx_msg void OnBnClickedBtnTools();
	afx_msg void OnBnClickedBtnHelp();
	afx_msg void OnBnClickedBtnClose();
	afx_msg void OnBnClickedBtnMinimize();
	afx_msg void OnBnClickedBtnGPL();
	afx_msg void OnShowWindow(BOOL bShow, UINT nStatus);
	afx_msg BOOL OnHelpInfo(HELPINFO* pHelpInfo);

	afx_msg LRESULT OnCmsStart            (WPARAM, LPARAM);
	afx_msg LRESULT OnCmsDone             (WPARAM, LPARAM);
	afx_msg LRESULT OnCmsSkipped          (WPARAM, LPARAM);
	afx_msg LRESULT OnCmsRandomizeAddrList(WPARAM, LPARAM);
	afx_msg LRESULT OnCmsSortAddrList     (WPARAM, LPARAM);
	afx_msg LRESULT OnCmsPhaseStart       (WPARAM, LPARAM);
	afx_msg LRESULT OnCmsPhaseDone        (WPARAM, LPARAM);
	afx_msg LRESULT OnCmsPhaseFailed      (WPARAM, LPARAM);
	afx_msg LRESULT OnCmsPhaseSkipped     (WPARAM, LPARAM);
	afx_msg LRESULT OnCmsAfterPhase       (WPARAM, LPARAM);
	afx_msg LRESULT OnCmsListBuilded      (WPARAM, LPARAM);
	afx_msg LRESULT OnCmsUpdateData       (WPARAM, LPARAM);
	afx_msg LRESULT OnCmsPacketsSent      (WPARAM, LPARAM);
	afx_msg LRESULT OnCmsMethodStart      (WPARAM, LPARAM);
	afx_msg LRESULT OnCmsMethodAbort      (WPARAM, LPARAM);
	afx_msg LRESULT OnCmsMethodSARPUpd    (WPARAM, LPARAM);
	afx_msg LRESULT OnCmsHuntMachine      (WPARAM, LPARAM);

	afx_msg LRESULT OnCmsAutoRun(WPARAM, LPARAM);
	afx_msg LRESULT OnCmsAutoClose(WPARAM, LPARAM);

	afx_msg LRESULT OnKickIdle(WPARAM, LPARAM);
	afx_msg void OnTimer(UINT_PTR nIDEvent);

	virtual BOOL OnCommand(WPARAM wParam, LPARAM lParam);

	DECLARE_MESSAGE_MAP()

public:
	DlgMain(CWnd* pParent = NULL);	// standard constructor
	~DlgMain(void);

	CString applicationPath;
	MEventLog *eventLog;

	CHScannerRunMode autoRunMode;
	CString          autoRunParameter;

	int     exitCode;
	CString exitRunningTime;
	CString exitExportedFile;

	enum { IDD = IDD_CHSCANNER_DIALOG };
};
